﻿
// ReminderRestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ReminderRest.h"
#include "DlgSet.h"
#include "afxdialogex.h"

//判断程序是否开机自动启动
BOOL IsAutoBoot()
{
	LPWSTR *szArglist = NULL;
	int nArgs = -1;
	BOOL bRet = FALSE;
	szArglist = CommandLineToArgvW(GetCommandLine(), &nArgs);
	if((NULL == szArglist) || (nArgs == 2))
	{
		if (_tcsicmp(szArglist[1], _T("-auto")) == 0)
		{
			bRet = TRUE;			//开机启动
		}
		else
		{
			bRet = FALSE;
		}
	}
	else
	{
		bRet = FALSE;
	}

	LocalFree(szArglist);

	return bRet;
}

//设置程序开机启动
BOOL AutoBootSet()
{
	if (IsAutoBoot())
		return TRUE;

	HKEY hKey; 

	//找到系统的启动项   
	CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); 

	//打开启动项Key   
	long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey);   
	if(lRet == ERROR_SUCCESS)   
	{ 
		TCHAR pFileName[MAX_PATH] = {0};

		//得到程序自身的全路径   
		DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH); 

		_tcscat(pFileName, _T(" -auto"));

		//添加一个子Key,并设置值, 下面应用程序名字（不加后缀.exe）  
		lRet = RegSetValueEx(hKey, _T("ReminderRest"), 0, REG_SZ, (LPBYTE)pFileName,  (lstrlen(pFileName) + 1)*sizeof(TCHAR));   

		//关闭注册表   
		RegCloseKey(hKey); 

		if(lRet != ERROR_SUCCESS)
		{
			return false;
		}
		return true;
	}
	return false;
}


//取消程序开机启动
BOOL AutoBootCancel()
{
	HKEY hKey;   

	//找到系统的启动项   
	CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); 

	long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey);   
	if(lRet == ERROR_SUCCESS)   
	{
		RegDeleteValue(hKey, _T("ReminderRest"));

		//关闭注册表   
		RegCloseKey(hKey);

		return true;  
	}
	return false;
}


// CDlgSet dialog

CDlgSet::CDlgSet(UINT nWorkTM,UINT nRestTM,UINT nSpeed,
	LPCTSTR lpszBack, bool bAuto, bool bAutoAudio, CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgSet::IDD, pParent)
	, m_nRestTime(nRestTM)
	, m_nWorkTime(nWorkTM)
	, m_strBackText(lpszBack)
	, m_nSpeed(nSpeed)
	, m_bAutoStart(bAuto)
	, m_bAutoPlayAudio(bAutoAudio)
{}


CDlgSet::~CDlgSet()
{
	
}


void CDlgSet::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_ET_RESTTIME, m_nRestTime);
	DDV_MinMaxUInt(pDX, m_nRestTime, 1, UINT_MAX);
	DDX_Text(pDX, IDC_ET_WORKTIME, m_nWorkTime);
	DDV_MinMaxUInt(pDX, m_nWorkTime, 1, UINT_MAX);
	DDX_Text(pDX, IDC_ET_BACKTEXT, m_strBackText);
	DDX_Text(pDX, IDC_ET_SPEED, m_nSpeed);
	DDV_MinMaxUInt(pDX, m_nSpeed, 1, UINT_MAX);
	DDX_Check(pDX, IDC_CHECK_AUTOSTART, m_bAutoStart);
	DDX_Check(pDX, IDC_CHECK_AUTOPLAYAUDIO, m_bAutoPlayAudio);
}

BEGIN_MESSAGE_MAP(CDlgSet, CDialogEx)
	ON_WM_CLOSE()
	ON_EN_CHANGE(IDC_ET_BACKTEXT, &CDlgSet::OnEnChangeEtBacktext)
END_MESSAGE_MAP()


void CDlgSet::OnEnChangeEtBacktext()
{
	UpdateData(TRUE);
	CT2A strA(m_strBackText);
	INT_PTR nLen = strlen(strA);
	strA[10] = 0;
	CA2T strT(strA);
	m_strBackText = strT;	
	UpdateData(FALSE);
	((CEdit*)GetDlgItem(IDC_ET_BACKTEXT))->SetSel(m_strBackText.GetLength(),m_strBackText.GetLength(),FALSE);
}

void CDlgSet::OnClose()
{
	UpdateData(TRUE);
	
	if (m_bAutoStart)
	{
		AutoBootSet();
	}
	else
	{
		AutoBootCancel();
	}

	CDialogEx::OnClose();
}


void CDlgSet::OnOK()
{
	UpdateData(TRUE);
	CDialogEx::OnOK();
}


UINT CDlgSet::GetRestTime()
{
	return m_nRestTime;
}

UINT CDlgSet::GetWorkTime()
{
	return m_nWorkTime;
}

LPCTSTR CDlgSet::GetBkText()
{
	return m_strBackText;
}

UINT CDlgSet::GetPlaySpeed()
{
	return m_nSpeed;
}

bool CDlgSet::GetAutoStart()
{
	return m_bAutoStart;
}

bool CDlgSet::GetAutoPlayAudio()
{
	return m_bAutoPlayAudio;
}
